<template>
  <div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
      <template #toolbar>
        <a-button
          type="primary"
          @click="handleCreate"
          v-if="hasPermission(['system:attendance-rule:create'])"
        >
          新增
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
              ifShow: hasPermission(['system:attendance-rule:update']),
            },
            {
              label: '删除',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              ifShow: hasPermission(['system:attendance-rule:delete']),
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <Drawer @register="registerDrawer" @success="handleSuccess" :type="type" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, nextTick, onMounted, reactive, toRefs } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import {
    getRuleList,
    createRule,
    deleteRule,
    editRule,
  } from '/@/api/backs/attendance/rule';
  import { useDrawer } from '/@/components/Drawer';
  import Drawer from './components/Drawer.vue';
  import { columns, searchFormSchema } from './components/tableData';
  import { message } from 'ant-design-vue';
  import { usePermission } from '/@/hooks/web/usePermission';

  export default defineComponent({
    name: 'AttendanceManagement',
    components: { BasicTable, Drawer, TableAction },
    setup() {
      const { hasPermission } = usePermission();
      const [registerDrawer, { openDrawer, closeDrawer }] = useDrawer();
      const [registerTable, { reload, expandAll, setTableData }] = useTable({
        title: '项目规则',
        api: getRuleList,
        columns,
        formConfig: {
          labelWidth: 100,
          schemas: searchFormSchema,
        },
        isTreeTable: false,
        pagination: true,
        // rowKey: 'projectId',
        striped: false,
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        canResize: true,
        actionColumn: {
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
          width: 160,
        },
      });
      const data: any = reactive({
        type: 'add',
      });
      //编辑
      const handleEditSuccess = async (formData) => {
        let params = { ...formData };
        try {
          await editRule(params);
          closeDrawer();
          reload();
          message.success('操作成功！');
        } catch (error) {}
      };
      //新增
      const handleAddSuccess = async (formData) => {
        let params = { ...formData };
        try {
          await createRule(params);
          closeDrawer();
          reload();
          message.success('操作成功！');
        } catch (error) {}
      };
      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
        data.type = 'add';
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
        data.type = 'edit';
      }
      function handleDelete(record: Recordable) {
        deleteRule({ ids: record.id }).then((res) => {
          if (!res) {
            message.success('删除成功！');
            reload();
          }
        });
      }

      function handleSuccess(formData) {
        switch (data.type) {
          case 'add':
            handleAddSuccess(formData);
            break;
          case 'edit':
            handleEditSuccess(formData);
            break;
        }
      }

      function onFetchSuccess() {
        // 演示默认展开所有表项
        // nextTick(expandAll);
      }

      return {
        ...toRefs(data),
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        onFetchSuccess,
        hasPermission,
      };
    },
  });
</script>
